package com.study.jyg.rxjava2study.activity;

import android.net.Uri;
import android.os.Bundle;

import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AppCompatActivity;

import com.study.jyg.rxjava2study.R;
import com.study.jyg.rxjava2study.fragment.SimpleFragment;
import com.study.jyg.rxjava2study.widget.ViewPagerIndicator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 带指示器的ViewPager
 */
public class ViewPagerActivity extends AppCompatActivity implements SimpleFragment.OnFragmentInteractionListener{

    @BindView(R.id.toolBar)
    Toolbar toolBar;
    @BindView(R.id.indicator)
    ViewPagerIndicator mIndicator;
    @BindView(R.id.vp_content)
    ViewPager mVp_content;

    private List<String> mTitles = Arrays.asList("标题1","标题2","标题3","标题4","标题5","标题6","标题7","标题8","标题9");
    private List<SimpleFragment> mFragmentContens = new ArrayList<>();
    private FragmentPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);
        ButterKnife.bind(this);
        setSupportActionBar(toolBar);
        toolBar.setNavigationOnClickListener(v -> finish());
        initViews();
        initDatas();
    }
    private void initDatas() {
        mIndicator.addIndicatorItem(mTitles);
        for (String title : mTitles){
            SimpleFragment fragment = SimpleFragment.newInstance(title, title);
            mFragmentContens.add(fragment);
        }

        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragmentContens.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentContens.size();
            }
        };
        mVp_content.setAdapter(mAdapter);
        mIndicator.setViewPager(mVp_content,0);
    }

    private void initViews(){
        mVp_content = (ViewPager)findViewById(R.id.vp_content);
        mIndicator = (ViewPagerIndicator)findViewById(R.id.indicator);
    }

    @Override
    public void onFragmentInteraction(Uri uri) {

    }
}
